<?php echo $ajax->form('ProductSerial', 'post', array('update'=> 'divProductSerial', 'indicator' => 'indicator', 'url'=>array('action'=>'save'))); ?>
<?php echo $form->hidden('ProductSerial.product_id', array('value' => $product_id)); ?>
<?php echo $form->label('Warehouse'); ?>
<?php echo $form->select('Location.warehouse_id', $warehouses, $selected_warehouse, null, '--All--'); ?>

<?php if (empty($this->data)) : // ถ้าไม่มีข้อมูล ?>
<?php echo $this->element('dataNotFound'); ?>
<? else : // ถ้ามีข้อมูล ?>

<table class="data">
    <thead>
        <tr>
            <th>No</th>
            <?php if($selected_warehouse == null) : ?>
            <th width="100px">Warehouse</th>
            <?php endif; ?>
            <th width="110px">Location</th>
            <th>Serial no.</th>
            <th>Other Information</th>
            <th>Cost</th>
            <th width="120px">Stock out Priority</th>
        </tr>
    </thead>
    <tbody>
        <?php $cur = 0; $sop = 1; foreach($this->data as $key => $data): ?>
        <tr class="<?php echo $cur % 2 == 0? 'even-cell': 'odd-cell'; ?>">
            <td class="center"><span><?php echo $cur + 1; ?></span></td>
            <?php if($selected_warehouse == null) { ?>
            <td>
                <?php echo !empty($data['Location']['id'])? $data['Location']['Warehouse']['name']: 'N/A'; ?>
            </td>
            <?php } ?>
            <td>
                <div id ="<?php echo "ProductSerialLocationId$cur"; ?>">
                    <?php echo !empty($data['Location']['id'])? $data['Location']['name']: 'N/A'; ?>
                </div>
            </td>
            <td><div id ="<?php echo "ProductSerialSerialNo$cur"; ?>"><?php echo !empty($data['ProductSerial']['serial_no'])? $data['ProductSerial']['serial_no']: 'N/A'; ?></div></td>
            <td><div id ="<?php echo "ProductSerialOtherInformation$cur"; ?>"><?php echo !empty($data['ProductSerial']['other_information'])? $data['ProductSerial']['other_information']: 'N/A'; ?></div></td>
            <td class="text-right"><?php echo number_format($data['ProductSerial']['cost'], 2); ?></td>
            <td class="center"><div id ="<?php echo "ProductSerialStockOutPriority$cur"; ?>"><?php echo $sop++; ?></div></td>
        </tr>
        <?php
        // ถ้ามีข้อมูล Location อยู่และยังไม่ได้ Dispatch จะเปลี่ยน Location ได้
        if (!empty($data['Location']['id']) && $data['ProductSerial']['its_stock_out_id'] == null) {
            echo $ajax->editor("ProductSerialLocationId$cur", "change_location/{$data['ProductSerial']['id']}", array('collection' => $locations[$data['Location']['warehouse_id']]));
        }
        echo $ajax->editor("ProductSerialSerialNo$cur", "inplace_editor/serial_no/{$data['ProductSerial']['id']}");
        echo $ajax->editor("ProductSerialOtherInformation$cur", "inplace_editor/other_information/{$data['ProductSerial']['id']}", array('rows' => 2, 'cols' => 50));
        // Special User เท่านั้นถึงจะมีสิทธิ์เปลี่ยน SoP ถ้าเป็น User ธรรมดา แสดง SoP เฉย ๆ
        if ($session->read('Auth.User.special_user')) {
            echo $ajax->editor("ProductSerialStockOutPriority$cur", "change_stock_out_priority/product_id:{$product_id}/{$data['ProductSerial']['id']}/{$data['ProductSerial']['stock_out_priority']}", array('collection' => $stock_out_priorities, 'clickToEditText' => 'Click for change Stock out Priority.'));
        }
        ++$cur;
        ?>
        <?php endforeach; ?>
    </tbody>
</table>
<?php endif; ?>
<?php echo $form->end(); ?>
<script type="text/javascript">
    $("LocationWarehouseId").observe('change', function() {
        var url = "products_serials/view/";
        url += $F("ProductSerialProductId") + "/";
        url += "warehouse_id:" + $F("LocationWarehouseId");
        requestUpdater('divProductSerial', url);
    });
</script>
<?php //debug($this->data); ?>